package com.macro.admin.controller;

import com.aliyuncs.exceptions.ClientException;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.macro.admin.pojo.RespBean;
import com.macro.admin.service.CaptchaService;
import com.macro.admin.utils.SmsUtils;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;

/**
 * 验证码controller
 *
 * @Author: Xiao Hai
 * @Date: 2021-04-21 08:01:07
 * @since 1.0.0
 */
@Api(tags = "验证码相关接口")
@RestController
public class CaptchaController {

    @Autowired
    private CaptchaService captchaService;



    @ApiOperation(value = "获取图片验证码")
    @GetMapping(value = "/captcha",produces = "image/jpeg")
    public void captcha(HttpServletRequest request, HttpServletResponse response){
        captchaService.captcha(request,response);
    }


    @ApiOperation(value = "获取短信验证码")
    @GetMapping("/smsCode/{phone}")
    public RespBean smsCode(@ApiParam(name = "phone",value = "手机号",required = true)@PathVariable String phone)  {
        int smsCode = captchaService.SmsCode(phone);
        if (smsCode == 0){
            return RespBean.error("验证码发送失败!");
        }
        return RespBean.success("验证码发送成功!");
    }
}
